<template>
    <div class="qingwu">
        <div class="admin_table_page_title">
            <a-button @click="$router.back()" class="float_right" icon="arrow-left">返回</a-button>
            公告编辑
        </div>
        <div class="unline underm"></div>
        <div class="admin_form">
            <a-form-model :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
                <a-form-model-item label="公告标题">
                    <a-input v-model="info.title"></a-input>
                </a-form-model-item>

                <a-form-model-item label="显示时间">
                    <a-range-picker :value="[info.start_time, info.end_time]" @change="time_format" format="YYYY-MM-DD HH:mm:ss" show-time></a-range-picker>
                </a-form-model-item>

                <a-form-model-item label="公告内容">
                    <a-input v-model="info.content" type="textarea" />
                </a-form-model-item>

                <a-form-model-item :wrapper-col="{ span: 12, offset: 5 }">
                    <a-button type="primary" @click="handleSubmit">提交</a-button>
                </a-form-model-item>
            </a-form-model>

        </div>
    </div>
</template>

<script>
import moment from 'moment';
export default {
    components: {},
    props: {},
    data() {
        return {
            info:{
            },
            list:[],
            id:0,
            loading:false,
        };
    },
    watch: {},
    computed: {},
    methods: {
        moment,
        handleSubmit(){

            // 验证代码处
            if(this.$isEmpty(this.info.title)){
                return this.$message.error('标题不能为空');
            }
            if(this.$isEmpty(this.info.content)){
                return this.$message.error('内容不能为空');
            }
            if(this.$isEmpty(this.info.start_time)){
                return this.$message.error('时间不能为空');
            }

            let api = this.$apiHandle(this.$api.adminAnnouncement,this.id);
            if(api.status){
                this.$put(api.url,this.info).then(res=>{
                    if(res.code == 200){
                        this.$message.success(res.msg)
                        return this.$router.back();
                    }else{
                        return this.$message.error(res.msg)
                    }
                })
            }else{
                this.$post(api.url,this.info).then(res=>{
                    if(res.code == 200){
                        this.$message.success(res.msg)
                        return this.$router.back();
                    }else{
                        return this.$message.error(res.msg)
                    }
                })
            }


        },
        get_info(){
            this.$get(this.$api.adminAnnouncement+'/'+this.id).then(res=>{
                this.info = res.data;
            })
        },
        // 获取菜单列表
        onload(){
            // 判断你是否是编辑
            if(!this.$isEmpty(this.$route.params.id)){
                this.id = this.$route.params.id;
                this.get_info();
            }

            // this.$get(this.$api.adminAdvPositions,{per_page:100}).then(res=>{
            //     this.list = res.data.data;
            // });

        },
        time_format(val){
            this.info.start_time = moment(val[0]._d).format("YYYY-MM-DD HH:mm:ss")
            this.info.end_time = moment(val[1]._d).format("YYYY-MM-DD HH:mm:ss")
            this.$forceUpdate();
        },
        upload(e){
            if(e.file.status == 'done'){
                this.loading = false;
                let rs = e.file.response;
                if(rs.code == 200){
                    this.$set(this.info,'image_url',rs.data);
                }else{
                    return this.$message.error(rs.msg);
                }
            }else{
                this.loading = true;
            }

        }


    },
    created() {
        this.onload();
    },
    mounted() {}
};
</script>
<style lang="scss" scoped>

</style>
